﻿/**
 *    Copyright 2006 - 2011 Opera Software ASA
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 **/


window.cls || ( window.cls = {} );

/**
  * @constructor 
  * A simple class to create a namespace on the window object.
  * It has one methode to add new created objects to it.
  */

window.cls.Namespace = function(namespace)
{
  if( window[namespace] instanceof this.constructor )
  {
    return window[namespace];
  }
  this.init(namespace);
}

window.cls.Namespace.prototype = new function()
{
  this.add = function(obj)
  {
    if( !(obj.id || obj.name) )
    {
      throw "to add an object to a namespace it must have either an id or a name";
    }
    return ( this[obj.id || obj.name] = obj );
  }
  this.init = function(namespace)
  {
    if( !( window[namespace] instanceof this.constructor ) )
    {
      window[namespace] = this;
    }
  }
  this.toString = function()
  {
    return "[object Namespace]";
  }

}
